home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
bit
/
src
/
ulib
/
split.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
2KB
|
83 lines
/***********************************************************************
* $Id: split.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
*
*. Copyright(c) 1993,1994 by T.C. Zhao
* All rights reserved.
*.
* Try to split a filename into directory and file components
* return -1 on error
***********************************************************************/
#if !defined(lint) && defined(F_ID)
char *id_split = "$Id: split.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
#endif
#include <stdio.h>
#include <string.h>
#include "unistd.h"
#include "ulib.h"
#define DO_STAT
#ifdef DO_STAT
#include <sys/types.h>
#include <sys/stat.h>
static struct stat buf;
#endif
int
split_fname(char dir[], char f[], const char *comb)
{
char *q;
char qq[PATH_MAX + 2];
/* expand dirname ~, ../ ./ etc to abs. path */
strcpy(qq, comb);
(void) fix_dirname(qq);
#ifdef DO_STAT
if (stat(qq, &buf))
{
perror(qq);
return -1;
}
if ((buf.st_mode & S_IFDIR) == S_IFDIR)
{
strcpy(dir, qq);
f[0] = '\0';
return 0;
}
if ((buf.st_mode & S_IFREG) != S_IFREG)
return -1;
#endif
if ((q = strrchr(qq, '/')) != 0)
{
strcpy(f, q + 1);
strncpy(dir, qq, q - qq);
dir[q - qq] = '\0';
}
else
return -1;
return 0;
}
#ifdef TEST
main()
{
char ll[1250], dir[1024], f[1024];
while (gets(ll))
{
if (split_fname(dir, f, ll) == 0)
fprintf(stderr, "dir=%s\t f=%s ext=%s\n", dir, f, getext(f));
else
fprintf(stderr, "bad stuff\n");
}
}
#endif